package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.GiftPackageService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.GiftPackageMapper;
import org.darkforest.model.GiftPackage;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by pc on 2017/4/6.
 */
@Controller
@RequestMapping(value="/giftpackage")
public class GiftPackageController extends WebController<GiftPackageController> {
    @Autowired
    private GiftPackageMapper giftPackageMapper;
    @Autowired
    private GiftPackageService giftPackageService;
    @RequestMapping(value="/list")
    public ModelAndView list(GiftPackage giftPackage){
        giftPackage.setPackageName(StringUtils.isBlank(giftPackage.getPackageName())?null:giftPackage.getPackageName());
        ModelAndView view = new ModelAndView("giftpackage/list");
        giftPackage = giftPackageService.selectByCondition(giftPackage);
        view.addObject("page",giftPackage);
        return view;
    }

    @RequestMapping(value="/toadd")
    public ModelAndView toadd(){
        ModelAndView view = new ModelAndView("giftpackage/add");
        return view;
    }

    @RequestMapping(value="/doadd")
    public String doadd(HttpServletRequest request,GiftPackage giftPackage,MultipartHttpServletRequest multipartHttpServletRequest){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftPackage.setCreaterId(sellerVo.getMemberId());
        giftPackage.setCreateName(sellerVo.getMemberName());
        giftPackage.setCreateTime(DateUtil.toTimeString(new Date()));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            giftPackage.setPackageImage(picture);
        }
        giftPackageMapper.insert(giftPackage);
        return "redirect:list";
    }

    @RequestMapping(value="/toedit")
    public ModelAndView toedit(Integer id){
        ModelAndView view = new ModelAndView("giftpackage/edit");
        GiftPackage giftPackage = giftPackageMapper.selectByPrimaryKey(id);
        view.addObject("giftPackage",giftPackage);
        return view;
    }

    @RequestMapping(value="/doedit")
    public String doedit(HttpServletRequest request,GiftPackage giftPackage,MultipartHttpServletRequest multipartHttpServletRequest){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftPackage.setCreaterId(sellerVo.getMemberId());
        giftPackage.setCreateName(sellerVo.getMemberName());
        giftPackage.setCreateTime(DateUtil.toTimeString(new Date()));
        MultipartFile pic = multipartHttpServletRequest.getFile("pic");
        if (pic.getSize() > 0) {
            String picture = super.uploadPicsWithoutCompressSize(pic, multipartHttpServletRequest, null, null);
            giftPackage.setPackageImage(picture);
        }
        giftPackageMapper.updateByPrimaryKeySelective(giftPackage);
        return "redirect:list";
    }
}
